package com.zyj.eps.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.zyj.eps.pojo.Knowledge;
import com.zyj.eps.service.KnowledgeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @author super
 * @date 2022/4/3 13:32
 */
@Controller
@RestController
@CrossOrigin
@SuppressWarnings("all")//抑制警告
public class KnowledgeController {
    @Autowired
    private KnowledgeServiceImpl knowledgeService;

    @RequestMapping("/getAllKnowledge")
    public String getAllKnowledge(int page){
        /*List<Knowledge> AKnowledge = knowledgeService.getAllKnowledge();*/
        List<Knowledge> allKnowledge = knowledgeService.getAllKnowledge();
        Collections.reverse(allKnowledge); //实现list集合逆序排列
        int i = allKnowledge.size();


        PageHelper.startPage(page, 7);
        List<Knowledge> allKnowledgeByPage = knowledgeService.getAllKnowledge();


        HashMap<String, Object> map = new HashMap<>();
        map.put("longi",i);
        map.put("allKnowledgeByPage",allKnowledgeByPage);
        map.put("allKnowledge",allKnowledge);
        return JSON.toJSONString(map);
    }
    @RequestMapping("/getKnowledgeById")
    public String getKnowledgeById(String id){
        int i = Integer.parseInt(id);
        Knowledge knowledgeByTitle = knowledgeService.getKnowledgeById(i);
        System.out.println(knowledgeByTitle);
        return JSON.toJSONString(knowledgeByTitle);
    }
}
